package com.wedo.mall.activity.main;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.e.library.activity.EActivity;
import com.e.library.listener.EListener;
import com.wedo.mall.R;
import com.wedo.mall.adapter.HistoryAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * 搜索
 * Created by lxy on 2017/6/20.
 */

public class SearchActivity extends EActivity {

    @BindView(R.id.btn_goods)   Button btn_goods;
    @BindView(R.id.v_goods)     View v_goods;
    @BindView(R.id.btn_shop)    Button btn_shop;
    @BindView(R.id.v_shop)      View v_shop;

    @BindView(R.id.et_search)   EditText et_search;
    @BindView(R.id.rv_search)   RecyclerView rv_search;
    @BindView(R.id.rv_history)  RecyclerView rv_history;
    @BindView(R.id.ll_history)  LinearLayout ll_history;

    @Override
    protected int layout() {
        return R.layout.activity_search;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        onGoodsClick();

        rv_history.setHasFixedSize(false);
        rv_history.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
        HistoryAdapter adapter = new HistoryAdapter(this,history());
        rv_history.setAdapter(adapter);
        adapter.setItemClickListener(new EListener<String>() {
            @Override
            public void onInvoked(View view, String data, int position) {
                et_search.setText(data);
            }
        });

    }

    private List<String> history(){
        List<String> histories = new ArrayList<>();
        histories.add("李白");
        histories.add("360行车仪");
        histories.add("是的啊");
        histories.add("韦德");
        histories.add("不值得你");
        histories.add("美猴王");
        histories.add("哈哈哈");
        histories.add("这是历史记录啊");
        histories.add("你喜欢就好");
        histories.add("这是WEdo？");

        return histories;

    }

    @OnClick(R.id.btn_cancel)
    protected void onCancelClick(){
        defaultFinish();
    }

    @OnClick(R.id.btn_goods)
    protected void onGoodsClick(){
        btn_goods.setSelected(true);
        v_goods.setVisibility(View.VISIBLE);
        btn_shop.setSelected(false);
        v_shop.setVisibility(View.INVISIBLE);
    }

    @OnClick(R.id.btn_shop)
    protected void onShopClick(){
        btn_goods.setSelected(false);
        v_goods.setVisibility(View.INVISIBLE);
        btn_shop.setSelected(true);
        v_shop.setVisibility(View.VISIBLE);
    }

    @OnTextChanged(R.id.et_search)
    protected void onTextChanged(Editable editable){
        String input = editable.toString();
        ll_history.setVisibility(TextUtils.isEmpty(input)?View.VISIBLE:View.GONE);
    }

}
